package com.zmn.oms.services.impl.orderencrypt;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zmn.anole.dubbo.interfaces.AnoleRemoteService;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.model.entity.master.OrderMaster;
import com.zmn.oms.services.interfaces.orderencrypt.OrderMasterEncryptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类描述：订单加解密服务
 *
 * @author heciqi
 * @date 2021/8/3 14:00
 */
@Slf4j
@Service
public class OrderMasterEncryptServiceImpl implements OrderMasterEncryptService {

	@Reference(version = AnoleRemoteService.VERSION, check = false)
	private AnoleRemoteService anoleRemoteService;

	/**
	 * 获取加密订单工程师
	 *
	 * @param orderMasters
	 */
	@Override
	public List<OrderMaster> getEncryptOrderMaster(List<OrderMaster> orderMasters) {
		if (CollectionUtil.isNullOrEmpty(orderMasters)) {
			return orderMasters;
		}

		List<OrderMaster> newOrderMasters = BeanMapper.mapList(orderMasters, OrderMaster.class);
		for (OrderMaster orderMaster : newOrderMasters) {
			if (StringUtil.isNotBlank(orderMaster.getMasterPhone())) {
				ResponseDTO<String> encryptRemote = anoleRemoteService.encrypt(orderMaster.getMasterPhone());
				log.debug("anoleRemoteService#encrypt 入参[{}],出参[{}]", orderMaster.getMasterPhone(), encryptRemote.getData());
				if (encryptRemote.isSuccess()) {
					orderMaster.setMasterPhone(encryptRemote.getData());
				} else {
					log.error("orderId[{}],masterPhone[{}],加密失败[{}]", orderMaster.getOrderId(),
							orderMaster.getMasterPhone(), encryptRemote.getMessage());
				}
			}
		}
		return newOrderMasters;
	}

	/**
	 * 解密订单工程师
	 *
	 * @param orderMasters
	 */
	@Override
	public void decryptOrderMaster(List<OrderMaster> orderMasters) {
		if (CollectionUtil.isNullOrEmpty(orderMasters)) {
			return;
		}

		for (OrderMaster orderMaster : orderMasters) {
			if (StringUtil.isNotBlank(orderMaster.getMasterPhone())) {
				ResponseDTO<String> encryptRemote = anoleRemoteService.decrypt(orderMaster.getMasterPhone());
				log.debug("anoleRemoteService#decrypt 入参[{}],出参[{}]", orderMaster.getMasterPhone(), encryptRemote.getData());
				if (encryptRemote.isSuccess()) {
					orderMaster.setMasterPhone(encryptRemote.getData());
				} else {
					log.error("orderId[{}],masterPhone[{}],解密失败[{}]", orderMaster.getOrderId(),
							orderMaster.getMasterPhone(), encryptRemote.getMessage());
				}
			}
		}
	}
}
